<template>
    <el-upload
      class="avatar-uploader"
      action="http://localhost:8080/upload/cover"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"   
      :headers="{'Access-Control-Allow-Origin':'*'}" 
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>
  </template>
  
  <script>
  export default{
    data(){
        return{
            imageUrl:''
        }
    },
    methods:{
        handleAvatarSuccess(res, file) {
            
             this.imageUrl = res;
            console.log(this.imageUrl);
            
          },
          beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 5;
    
            if (!isJPG) {
              this.$message.error('上传头像图片只能是 JPG格式!');
            }
            if (!isLt2M) {
              this.$message.error('上传头像图片大小不能超过 5MB!');
            }
            return isJPG && isLt2M;
          }
        },
    }
  
  </script>
  
  <style scoped>
  .avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: inline-block;
  }
  </style>
  
  <style>
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }
  
  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }
  
  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
  </style>
  